package com.eshop.modules.shop.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import java.io.Serializable;
import java.util.Arrays;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.modules.aop.ForbidSubmit;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.shop.domain.AppVersion;
import org.springframework.http.HttpStatus;
import com.eshop.domain.PageResult;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import java.io.IOException;
import com.eshop.modules.shop.service.dto.AppVersionDto;
import com.eshop.modules.shop.service.dto.AppVersionQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.shop.service.AppVersionService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "app版本控制管理" })
@RestController
@RequestMapping({ "/api/yxAppVersion" })
public class YxAppVersionController
{
    private final AppVersionService appVersionService;
    private final IGenerator generator;
    
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('admin','yxAppVersion:list')")
    public void download(final HttpServletResponse response, final AppVersionQueryCriteria criteria) throws IOException {
        this.appVersionService.download(this.generator.convert(this.appVersionService.queryAll(criteria), (Class)AppVersionDto.class), response);
    }
    
    @GetMapping
    @Log("查询app版本控制")
    @ApiOperation("查询app版本控制")
    @PreAuthorize("@el.check('admin','yxAppVersion:list')")
    public ResponseEntity<PageResult<AppVersionDto>> getYxAppVersions(final AppVersionQueryCriteria criteria, final Pageable pageable) {
        return (ResponseEntity<PageResult<AppVersionDto>>)new ResponseEntity(this.appVersionService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @ForbidSubmit
    @PostMapping
    @Log("新增app版本控制")
    @ApiOperation("新增app版本控制")
    @PreAuthorize("@el.check('admin','yxAppVersion:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody final AppVersion resources) {
        return new ResponseEntity(this.appVersionService.save(resources), HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @PutMapping
    @Log("修改app版本控制")
    @ApiOperation("修改app版本控制")
    @PreAuthorize("@el.check('admin','yxAppVersion:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final AppVersion resources) {
        this.appVersionService.updateById(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除app版本控制")
    @ApiOperation("删除app版本控制")
    @PreAuthorize("@el.check('admin','yxAppVersion:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody final Integer[] ids) {
        Arrays.asList(ids).forEach(id -> this.appVersionService.removeById((Serializable)id));
        return new ResponseEntity(HttpStatus.OK);
    }
    
    public YxAppVersionController(final AppVersionService appVersionService, final IGenerator generator) {
        this.appVersionService = appVersionService;
        this.generator = generator;
    }
}
